package com.blocks.mq.producer.example.service;

import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * @author lxd
 * @date 2020/12/5 9:39
 */
@Service
public class ExampleTxtReadService {
    public String readTxtData(String path) {
        StringBuilder result = new StringBuilder();
        try (FileInputStream fis = new FileInputStream(new File(path));
             InputStreamReader fr = new InputStreamReader(fis, StandardCharsets.UTF_8);
             BufferedReader br = new BufferedReader(fr)) {
            String s;
            while ((s = br.readLine()) != null) {
                result.append(s).append(System.lineSeparator());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }
}
